package cn.jizi.bshisy.controller;

import cn.jizi.bshisy.entity.Drug;
import cn.jizi.bshisy.service.DrugService;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 李鹏辉Code
 * @create 2020 -10 -31 13:54
 */

@RestController
@RequestMapping("/drug")
public class DrugController {

    @Autowired
    private DrugService drugService;


    @ApiOperation(value = "已有药品出库", notes = "根据did找到药品做出库")
    @PutMapping(value = "/drugDelivery")
    public String drugDelivery(String did, Integer num) {
        if (drugService.drugDelivery(did, num))
            return "已有药品出库成功";
        return "已有药品出库失败";
    }

    @ApiOperation(value = "已有药品入库", notes = "根据已有的did找到药品做入库 主要是现有库存加上新入库库存")
    @PutMapping(value = "/drugWarehouse")
    public String drugWarehouse(Drug drug, Integer newNum) {
        if (drugService.drugWarehouse(drug, newNum))
            return "已有药品入库成功";
        return "已有药品入库失败";
    }

    @ApiOperation(value = "新药品入库", notes = "插入新的药品")
    @PostMapping(value = "/newDrugs")
    public String newDrugs(Drug drug) {
        if (drugService.newDrugs(drug))
            return "新药品入库成功";
        return "新药品入库失败";
    }
    @ApiOperation(value = "查询药品数量小于5的", notes = "前台设置定时器每天定时查询药品数量低于5的进行补货如果为空者不需要补货")
    @GetMapping("/findByNum")
    @ResponseBody
    public Object findByNum(){
        List<Drug> list= drugService.findByNum();
        String json= JSONArray.toJSONString(list);
        if (null!=list){
            return json;
        }else {
            return json;
        }
    }
}
